import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";

function WhileList({ children }) {
  function NewCap() {
    const navigate = useNavigate();
    const { pathName } = useLocation();
    const token = localStorage.getItem("token");
    const WhileLists = ["/", "/login"];

    useEffect(() => {
      if (!token && !WhileLists.includes(pathName)) {
        navigate("/login");
      }
    }, [token, pathName]);
    return children;
  }
  return <NewCap></NewCap>;
}

export default WhileList;
